package com.web.exiaodao.business.service.impl;

import java.util.Map;

import com.web.exiaodao.business.entity.ServiceDomesticNursing;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;
import com.web.exiaodao.business.repository.ServiceDomesticNursingRepository;
import com.web.exiaodao.business.service.DomesticNursingService;

@Service("DomesticNursingService")
@Transactional(readOnly = false)
public class DomesticNursingServiceImpl extends BaseService<ServiceDomesticNursing> implements DomesticNursingService {

	private static final Logger LOGGER = Logger.getLogger(DomesticNursingServiceImpl.class);

	@Autowired
	private ServiceDomesticNursingRepository serviceDomesticNursingRepository;

	/**
	 * 查询单条DomesticNursing信息
	 * //@param tId
	 * @return 返回单个DomesticNursing对象
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<ServiceDomesticNursing> findOne(Long modelId) throws BusinessException {
		ServiceDomesticNursing obj = new ServiceDomesticNursing();
		if(modelId != 0){
			obj = serviceDomesticNursingRepository.findByIdAndStatusNot(modelId,XaConstant.Status.delete);
		}
		XaResult<ServiceDomesticNursing> xr = new XaResult<ServiceDomesticNursing>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(obj);
			LOGGER.info("后台----->查询单个实体----->ServiceDomesticNursing");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	/**
	 * 分页查询状态非status的DomesticNursing数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象DomesticNursing集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ServiceDomesticNursing>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<ServiceDomesticNursing> page = serviceDomesticNursingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceDomesticNursing.class), pageable);
		XaResult<Page<ServiceDomesticNursing>> xr = new XaResult<Page<ServiceDomesticNursing>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态不等于）----->ServiceDomesticNursing");
		return xr;
	}

	/**
	 * 分页查询状态status的DomesticNursing数据
	 * @param status
	 * @param filterParams
	 * @param pageable
	 * @return 返回对象DomesticNursing集合
	 * @throws BusinessException
	 */
	@Transactional(readOnly = true, rollbackFor = Exception.class)
	public XaResult<Page<ServiceDomesticNursing>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable) throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<ServiceDomesticNursing> page = serviceDomesticNursingRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), ServiceDomesticNursing.class), pageable);
		XaResult<Page<ServiceDomesticNursing>> xr = new XaResult<Page<ServiceDomesticNursing>>();
		xr.setObject(page);
		LOGGER.info("后台----->分页查询实体集合（状态等于）----->ServiceDomesticNursing");
		return xr;
	}

	/**
	 * 保存DomesticNursing信息
	 * @param model
	 * @return
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ServiceDomesticNursing> saveOrUpdate(ServiceDomesticNursing model) throws BusinessException {
		ServiceDomesticNursing obj = null;
		if(XaUtil.isNotEmpty(model.getId())){
			obj = serviceDomesticNursingRepository.findOne(model.getId());
		}else{
			obj = new ServiceDomesticNursing();
		}
		if(model.getNurse_Inforation().length()!=0){
			String nurse_Inforation=  model.getNurse_Inforation().replace("<p>", "");
			nurse_Inforation=  nurse_Inforation.replace("</p>", "");
			model.setNurse_Inforation(nurse_Inforation);
		}
		
		obj.setNurse_Name(XaUtil.isNotEmpty(model.getNurse_Name()) ? model.getNurse_Name() : obj.getNurse_Name());
		obj.setNurse_RoleID(XaUtil.isNotEmpty(model.getNurse_RoleID()) ? model.getNurse_RoleID() : obj.getNurse_RoleID());
		obj.setNurse_Sex(XaUtil.isNotEmpty(model.getNurse_Sex()) ? model.getNurse_Sex() : obj.getNurse_Sex());
		obj.setNurse_Age(XaUtil.isNotEmpty(model.getNurse_Age()) ? model.getNurse_Age() : obj.getNurse_Age());
		obj.setNurse_LengthServi(XaUtil.isNotEmpty(model.getNurse_LengthServi()) ? model.getNurse_LengthServi() : obj.getNurse_LengthServi());
		obj.setNurse_Native(XaUtil.isNotEmpty(model.getNurse_Native()) ? model.getNurse_Native() : obj.getNurse_Native());
		obj.setNurse_Salary(XaUtil.isNotEmpty(model.getNurse_Salary()) ? model.getNurse_Salary() : obj.getNurse_Salary());
		obj.setNurse_PicUrl(XaUtil.isNotEmpty(model.getNurse_PicUrl()) ? model.getNurse_PicUrl() : obj.getNurse_PicUrl());
		obj.setNurse_Cellphone(XaUtil.isNotEmpty(model.getNurse_Cellphone()) ? model.getNurse_Cellphone() : obj.getNurse_Cellphone());
		obj.setNurse_Education(XaUtil.isNotEmpty(model.getNurse_Education()) ? model.getNurse_Education() : obj.getNurse_Education());
		obj.setNurse_Salary_unit(XaUtil.isNotEmpty(model.getNurse_Salary_unit()) ? model.getNurse_Salary_unit() : obj.getNurse_Salary_unit());
		obj.setNurse_Inforation(XaUtil.isNotEmpty(model.getNurse_Inforation()) ? model.getNurse_Inforation() : obj.getNurse_Inforation());
		obj.setSort(XaUtil.isNotEmpty(model.getSort()) ? model.getSort() : obj.getSort());
		obj = serviceDomesticNursingRepository.save(obj);
		XaResult<ServiceDomesticNursing> xr = new XaResult<ServiceDomesticNursing>();
		xr.setObject(obj);
		LOGGER.info("后台新增或编辑数据成功！");
		return xr;
	}

	/**
	 * 修改DomesticNursing状态，可一次修改多条   3删除  -1锁定  1正常
	 * //@param userId
	 * @param modelIds
	 * @param status
	 * @return 返回DomesticNursing对象
	 * @throws BusinessException
	 */
	@Transactional(rollbackFor = Exception.class)
	public XaResult<ServiceDomesticNursing> multiOperate(
			String modelIds,Integer status) throws BusinessException {
		XaResult<ServiceDomesticNursing> xr = new XaResult<ServiceDomesticNursing>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				ServiceDomesticNursing obj = serviceDomesticNursingRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = serviceDomesticNursingRepository.save(obj);
				}
			}
		}
		LOGGER.info("批量修改数据状态成功！");
		return xr;
	}

}
